Add a Fatal() and Warning() that take stream operators so we can replace
authorrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 8 Jun 2014 04:22:44 +0000 (04:22 +0000)
committerrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 8 Jun 2014 04:22:44 +0000 (04:22 +0000)
those billion hardcoded calls to sprintf-like substances that use CSTR.

gpsbabel/GPSBabel.pro
gpsbabel/logging.h [new file with mode: 0644]
gpsbabel/waypt.cc

index 5e5c4d50886e9db514792b6ef95bcdfa0454f3b2..a785781514598d5086f42bf04eab203c880e42a7 100644 (file)
@@ -215,6 +215,7 @@ HEADERS =  \
        jeeps/gpsusbcommon.h \
        jeeps/gpsusbint.h \
        jeeps/gpsutil.h \
+       logging.h \
        magellan.h \
        mapsend.h \
        navilink.h \
diff --git a/gpsbabel/logging.h b/gpsbabel/logging.h
new file mode 100644 (file)
index 0000000..9226a1c
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+    Copyright (C) 2014 Robert Lipe, robertlipe+source@gpsbabel.org
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
+
+ */
+#ifndef gpsbabel_logging_h_included
+#define gpsbabel_logging_h_included
+
+// A wrapper for QTextStream that provides a sensible Warning() and Fatal()
+// with convenient stream operators.
+
+#include <QtCore/QTextStream.h>
+#include <QtCore/QFile.h>
+
+class Warning {
+ public:
+  Warning(bool fatal = false) :
+   fatal_(fatal) {
+    file_.open(stderr, QIODevice::WriteOnly);
+    fileStream_.setDevice(&file_);
+  }
+  ~Warning() {
+    fileStream_ << '\n';
+    if (fatal_) {
+      fileStream_.flush();
+      exit(1);
+    }
+  }
+  inline Warning& operator << (char d) { fileStream_ << d; return optionalSpace(); }
+  inline Warning& operator << (signed short d) { fileStream_ << d; return optionalSpace(); }
+  inline Warning& operator << (unsigned short d) { fileStream_ << d; return optionalSpace(); }
+  inline Warning& operator << (signed int d) { fileStream_ << d; return optionalSpace(); }
+  inline Warning& operator << (unsigned int d) { fileStream_ << d; return optionalSpace(); }
+  inline Warning& operator << (signed long d) { fileStream_ << d; return optionalSpace(); }
+  inline Warning& operator << (unsigned long d) { fileStream_ << d; return optionalSpace(); }
+  inline Warning& operator << (float d) { fileStream_ << d; return optionalSpace(); }
+  inline Warning& operator << (double d) { fileStream_ << d; return optionalSpace(); }
+  inline Warning& operator << (const char* d) { fileStream_ << QString::fromUtf8(d); return optionalSpace(); }
+  inline Warning& operator << (QString d) { fileStream_ << '\"' << d << '\"'; return optionalSpace(); }
+  inline Warning& operator << (const void* d) { fileStream_ << '\"' << d << '\"'; return optionalSpace(); }
+
+  inline Warning& optionalSpace() {
+    fileStream_ << ' ';
+    return *this;
+  }
+private:
+  QFile file_;
+  QTextStream fileStream_;
+  bool fatal_;
+};
+
+class Fatal : public Warning {
+ public:
+  Fatal() : Warning(true) {}
+};
+
+#endif //  gpsbabel_logging_h_included
index c2c022e34a0a309ca436b3367421d0b817d57408..44b8608312ddedf2b1a6504741b43d4e833f7fb8 100644 (file)
@@ -30,6 +30,8 @@
 #include "garmin_fs.h"
 #include "session.h"
 
+#include <logging.h>
+
 #if NEWQ
 QList<Waypoint*> waypt_list;
 queue waypt_head; // This is here solely to freak out the formats that are
@@ -100,12 +102,12 @@ waypt_add(Waypoint* wpt)
   }
 
   if ((wpt->latitude < -90) || (wpt->latitude > 90.0))
-    fatal("%s: Invalid latitude %f in waypoint '%s'.\n",
-          wpt->session->name,
-          lat_orig, CSTRc(wpt->shortname));
+    Fatal() << wpt->session->name 
+            << "Invalid latitude" << lat_orig << "in waypoint" 
+            << wpt->shortname;
   if ((wpt->longitude < -180) || (wpt->longitude > 180.0))
-    fatal("Invalid longitude %f in waypoint '%s'.\n",
-          lon_orig, CSTRc(wpt->shortname));
+    Fatal() << "Invalid longitude" << lon_orig << "in waypoint" 
+            << wpt->shortname;
 
   /*
    * Some input may not have one or more of these types so we